#include "DungeonsListScene.h"
#include "../GlobalLayer.h"
#include "../DungeonsItemLoader.h"
#include "DungeonsListSceneLoader.h"
#include "../UserInfoLoader.h"
#include "../MainMenuLoader.h"
#include "../../../model/static/StaticDungeons.h"
#include "../../../model/proxy/DungeonsProxy.h"

DungeonsListScene::DungeonsListScene(void)
	:mReturn(NULL),mNextBtn(NULL),mLastBtn(NULL),mPageCount(NULL),mScrollView(NULL)
{
}

DungeonsListScene::~DungeonsListScene(void)
{
	/*CC_SAFE_RELEASE(mReturn);
	CC_SAFE_RELEASE(mNextBtn);
	CC_SAFE_RELEASE(mLastBtn);
	CC_SAFE_RELEASE(mPageCount);*/
	//CC_SAFE_RELEASE(mScrollView);
}

SEL_MenuHandler DungeonsListScene::onResolveCCBCCMenuItemSelector( cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName )
{
	return NULL;
}

SEL_CCControlHandler DungeonsListScene::onResolveCCBCCControlSelector( cocos2d::CCObject * pTarget, cocos2d::CCString * pSelectorName )
{
	return NULL;
}

bool DungeonsListScene::onAssignCCBMemberVariable( cocos2d::CCObject * pTarget, cocos2d::CCString * pMemberVariableName, cocos2d::CCNode * pNode )
{
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mScrollView", CCScrollView *, this->mScrollView);
	return true;
}


void DungeonsListScene::onNodeLoaded( cocos2d::CCNode * pNode, cocos2d::extension::CCNodeLoader * pNodeLoader )
{
	mScrollView = CCScrollView::create(CCSizeMake(320, 320));
	mScrollView->setDirection(CCScrollViewDirectionVertical);
	mScrollView->setPosition(ccp(0, 64));
	addChild(mScrollView);
}

bool DungeonsListScene::init()
{
	State::init();
	return true;
}

void DungeonsListScene::onEnter()
{
	State::onEnter();
	refresh();
}

void DungeonsListScene::onExit()
{
	State::onExit();
}

void DungeonsListScene::menuReturnCallback( CCObject* pSender )
{
	Framework::getSingletonPtr()->changeState("DungeonsScene");
}

void DungeonsListScene::refresh()
{
	RegisterLoader("DungeonsItem", DungeonsItemLoader::loader());
	FloorStaticMap floorMap = DungeonsProxy::getSingletonPtr()->getCurDungeonsStatic()->floorList;
	FloorStaticMap::iterator iter = floorMap.begin();
	int maxCount = floorMap.size();
	CCPoint contentPos = ccp(0, maxCount * 90);
	mContent = CCLayer::create();
	mContent->setContentSize(CCSizeMake(320, contentPos.y));
	mScrollView->setContainer(mContent);
	mScrollView->setContentOffset(ccp(0, mScrollView->getViewSize().height-contentPos.y));
	int count = 0;
	for (; iter != floorMap.end(); iter++)
	{
		DungeonsItem* pNode = (DungeonsItem*)ReadNode("DungeonsItem.ccbi", this);
		pNode->dungeonsID = DungeonsProxy::getSingletonPtr()->getCurDungeonsID();
		pNode->floorID = iter->first;
		pNode->refresh();
		pNode->setPosition(ccp(0, contentPos.y - (++count * 90)));
		mContent->addChild(pNode);
	}
}


